package com.unity.bh3.util;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * 时间工具类
 */
public class TimeUtil {
    
    /**
     * 将时间转换为相对时间格式（如：4小时前、4分钟前）
     * @param dateTime 要转换的时间
     * @return 相对时间字符串
     */
    public static String formatRelativeTime(LocalDateTime dateTime) {
        if (dateTime == null) {
            return "未知";
        }
        
        LocalDateTime now = LocalDateTime.now();
        
        // 计算时间差
        long years = ChronoUnit.YEARS.between(dateTime, now);
        if (years > 0) {
            return years + "年前";
        }
        
        long months = ChronoUnit.MONTHS.between(dateTime, now);
        if (months > 0) {
            return months + "个月前";
        }
        
        long days = ChronoUnit.DAYS.between(dateTime, now);
        if (days > 0) {
            return days + "天前";
        }
        
        long hours = ChronoUnit.HOURS.between(dateTime, now);
        if (hours > 0) {
            return hours + "小时前";
        }
        
        long minutes = ChronoUnit.MINUTES.between(dateTime, now);
        if (minutes > 0) {
            return minutes + "分钟前";
        }
        
        long seconds = ChronoUnit.SECONDS.between(dateTime, now);
        if (seconds > 0) {
            return seconds + "秒前";
        }
        
        return "刚刚";
    }
}